import dayjs from 'dayjs'
import { useRouterStore, useUserStore } from '@/store'
import { findTree } from 'xe-utils'

/**
 * 是否只null和undefined值
 * @param vl
 * @returns
 */
export const isNullOrUndefined = (vl: unknown): boolean => {
  return vl === null || typeof vl === 'undefined'
}

/**
 * 计算两时间相差x年x天x小时
 * @param start
 * @param end
 * @returns
 */
export const calDuration = (start: Date, end: Date) => {
  // 计算两个日期之间的总天数
  const totalDays = dayjs(end).diff(dayjs(start), 'days')

  // 计算总小时数
  const totalHours = dayjs(end).diff(dayjs(start), 'hours')

  // 计算年数、剩余天数和小时数
  const years = Math.floor(totalDays / 365)
  const remainingDays = totalDays % 365
  const hours = Math.floor((totalHours - years * 365 * 24) % 24)

  // 构建结果字符串
  let result = ''
  if (years > 0) result += `${years}年`
  if (remainingDays > 0) result += `${remainingDays}天`
  if (hours > 0) result += `${hours}小时`

  return result
}

/**不需要判断的路由 */
const unPatchRoutePathlist = ['/fault/statistic-customer/cpns/view-list-all', '/login', '/agent/statistic/cpns/view-list-all']
/**
 * 是否有对应菜单路由
 * @param path
 * @returns
 */
export const hasRoute = (path: string) => {
  if (unPatchRoutePathlist.includes(path)) return true
  const { routerList } = useRouterStore()
  const findData = findTree([...routerList], item => item.path === path)
  if (findData) return true
  return false
}

/**
 * 是否有权限
 * @param permis
 * @returns
 */
export const hasPermi = (permis: string[]) => {
  if (!permis.length) return false
  const { permission } = useUserStore()
  return permis.some(p => permission.includes(p))
}
